package com.qwt.part_time_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qwt.part_time_api.error.BusinessException;
import com.qwt.part_time_api.error.ErrorCode;
import com.qwt.part_time_api.mapper.RegionMapper;
import com.qwt.part_time_api.pojo.Region;
import com.qwt.part_time_api.service.RegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 君子慎独
 * @create 2022/1/20 0020 17:20
 */
@Service
@Slf4j
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public String findRegionCodeByName(String name) throws BusinessException {
        log.info("获取城市编码");
        if (name == null) {
            log.info("城市为空");
            throw new BusinessException(ErrorCode.ERR_NO_NULL);
        }
        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Region::getRegionCode);
        wrapper.eq(Region::getName, name);
        Region region = regionMapper.selectOne(wrapper);
        if (region == null) {
            log.info("无该城市");
            throw new BusinessException(ErrorCode.ERR_NO_SUCH_CITY);
        }
        return region.getRegionCode();
    }
}
